package cn.dlc.com.config;

import cn.dlc.com.account.service.AccountService;
import cn.dlc.com.log.entity.AccountChangeEvent;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 邓立川
 * @date 2021/1/28 15:13
 * @desc 监听消息
 */
@Slf4j
@Component
// 注意和监听事务消息的注解不一样，
// consumerGroup：消费者组
// topic和消息发送的topic一样
@RocketMQMessageListener(consumerGroup="tx-consumer-group",topic = "transfer-topic")
public class MsgListener implements RocketMQListener<String> {

    @Autowired
    private AccountService accountService;

    @Override
    public void onMessage(String message) {

        log.info("收到消息{}",message);

        // 获取消息进行转换
        AccountChangeEvent accountChangeEvent = JSON.parseObject(message, AccountChangeEvent.class);

        // 调用本地事务
        accountService.transfer(accountChangeEvent);

    }
}
